package br.ufrj.ppgi.jemf_advanced_example.equipment;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Equipment;
import br.ufrj.ppgi.jemf.mobile.view.EquipmentView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Equipment detail screen.
 * This fragment is either contained in a {@link EquipmentListActivity} in two-pane mode (on tablets) or a {@link EquipmentDetailActivity} on handsets.
 */
public class EquipmentDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_EQUIPMENT = "activated_equipment";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "EQUIPMENT";
    /**
     * The Equipment content this fragment is showing.
     */
    private static Equipment equipment;
    /**
     * The Equipment view holder.
     */
    private static EquipmentView equipmentView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public EquipmentDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Equipment content specified by the fragment arguments.
        if (getArguments().containsKey(EquipmentDetailFragment.ARG_ID)) {
        	equipment = getArguments().getParcelable(EquipmentDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Equipment content
	    if (equipment != null) {
	    	equipmentView = new EquipmentView(getActivity(), false);
	    	// Name
	    	equipmentView.setEquipmentNameText(equipment.getName());
	    	// Description
	    	equipmentView.setEquipmentDescriptionText(equipment.getDescription());
	    	// Status
	    	equipmentView.setEquipmentStatusText(equipment.getStatusString());

	    	// Its necessary to add the EquipmentView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Equipment View as a child of the ScrollView.
	    	scrollView.addView(equipmentView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Equipment
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_EQUIPMENT)) {
        	equipment = savedInstanceState.getParcelable(STATE_EQUIPMENT);
        }
	}





	/**
     * Get updated Equipment
     * @return Equipment
     */
	public static Equipment getEquipment() {
		if (BuildConfig.DEBUG) Log.i(EquipmentListActivity.TAG, "EquipmentDetailFragment ::: getEquipment");
    	return equipment;
    }

	/**
	 * Update Equipment data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((equipment != null) && (equipmentView != null)) {
    		if (BuildConfig.DEBUG) Log.i(EquipmentListActivity.TAG, "EquipmentDetailFragment ::: updateDataFromUI");
    		// Update Equipment object with new data included by user through components
    		equipment.setName(equipmentView.getEquipmentNameText());
    		equipment.setDescription(equipmentView.getEquipmentDescriptionText());
    		equipment.setStatusString(equipmentView.getEquipmentStatusText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Equipment.
        outState.putParcelable(STATE_EQUIPMENT, equipment);
    }
}
